
getwd()
setwd("/Users/Desktop")
setwd("/Users/vicissitude_86/Documents//Abortion/Dataset/spss/spss") #### Mac Pro ###
getwd()

setwd("/Users/Aiden/Documents/cambridge_copy/Replicationcourse/Abortion/Dataset/spss/spss") #### IMAC ####
getwd()

setwd("c:/Users/Aiden/Downloads/Abortion/Abortion/Dataset/spss/spss") #### Desktop ###

library(foreign) # in order to read spss files. 
natsal1 <- read.spss("natsal.por", use.value.labels=TRUE, to.data.frame=TRUE) #.por = SPSS portable File 

##################################################### TABLE 3 #####################################################################
##### ABORTION RATES ##################################################################################################

#### Yes/No Abortion variables #############
natsal1[289]
natsal1[["MANYAB"]] #Number of times women have abortion. 
table(natsal1$ABORT, natsal1$MANYAB)
dataset1 <- natsal1[,c("ABORT","MANYAB")] 
which(dataset1$ABORT == "yes" | dataset1$ABORT=="no")  
dataset3 <- dataset1[which(dataset1$ABORT == "yes" | dataset1$ABORT=="no") ,] 
table(dataset3)
dataset3$ABORT <- droplevels(dataset3$ABORT)
table(dataset3)

#### 2 or more Abortion rates combined ############### 

natsal1$manyab1 <- natsal1$MANYAB ##new variable will be added to the dataset. # i need to specify that all the values = 1, so the new value will be NA. 
natsal1$manyab1[natsal1$manyab1==-1] <- NA # So [] selects all the collection of elements of the specified variable. 
natsal1$manyab1
natsal1$manyab1[natsal1$manyab1==99] <- NA
natsal1$manyab1
natsal1$manyab1[natsal1$manyab1>2] <-3
natsal1$manyab1
natsal1$manyab2 <- natsal1$manyab1

table(natsal1$manyab2)
natsal1$manyab2[natsal1$manyab2>1] <- 2
#####natsal1$manyab1[natsal1$manyab1>1] <- 2  ## I don't understand why when you insert this code in, the variables from column 3 gets taken out. It is better without this line anyway, but i wanted to understand. 
table(natsal1$manyab2)

natsal2$manyab3 <- natsal1$manyab2
natsal2 <- natsal1[natsal1$manyab2!=0,]
View(natsal2)
table(natsal2$manyab2)

############# 1 or more abortion rates ##############

natsal1$allabort <- natsal1$MANYAB
natsal1$allabort[natsal1$allabort==-1] <-NA
natsal1$allabort[natsal1$allabort==99] <-NA
natsal1$allabort[natsal1$allabort>0] <- 1

natsal1$allabort1 <- as.factor(natsal1$allabort)
help(as.factor)
summary(natsal1$allabort1)

##### ABORTION RATE (END) ###########################

######## AGE AT FIRST ABORTION (YEARS - START) ###########################

natsal1[[291]]
natsal1$AGEAB
table(natsal1$ABORT, natsal1$AGEAB)
dataset4 <- natsal1[,c("ABORT", "AGEAB")]
which(dataset4$ABORT == "yes" | dataset4$ABORT == "no")
dataset5 <- dataset4[which(dataset4$ABORT=="yes"| dataset4$ABORT=="no"),]
table(dataset5)
dataset5$ABORT <- droplevels(dataset5$ABORT)
table(dataset5)

######## Categorise into age group ######################

natsal1$ageab1 <- natsal1$AGEAB
natsal1$ageab1
agecat <- subset (natsal1, ageab1>=14 & ageab1<=42)$ageab1
## subset1 <- subset(dataset1,GAFSEX == "16-17" | dataset1$GAFSEX == "18-19", select=c(ABORT, GAFSEX)) How are they different?
range(agecat)

ageabcat1 <- cut(agecat, seq(12,15,3), right=F, include.lowest=T) #include.lowest=T, makes the first interval close at both ends. pg. 164
ageabcat2 <- cut(agecat, seq(16,17,1), right=F, include.lowest=T) 
ageabcat3 <- cut(agecat, seq(18,19,1), right=F, include.lowest=T) 
ageabcat4 <- cut(agecat, seq(20,24,4), right=F, include.lowest=T) 
ageabcat5 <- cut(agecat, seq(25,29,4), right=F, include.lowest=T) 
ageabcat6 <- cut(agecat, seq(30,34,4), right=F, include.lowest=T) 
ageabcat7 <- cut(agecat, seq(35,39,4), right=F, include.lowest=T) 
ageabcat8 <- cut(agecat, seq(40,49,9), right=F, include.lowest=T) 

# The result doesn't look promising because you can see from table 1,2,3 that the number of abortion rate is always lower even before adjusting for the sampling size. 
table(ageabcat1)
table(ageabcat2)
table(ageabcat3)
table(ageabcat4)
table(ageabcat5)
table(ageabcat6)
table(ageabcat7)
table(ageabcat8)

####################### AGE AT FIRST ABORTION (YEARS - END) ########################################################################
####################################################################################################################################
